<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<link href="style.css" rel="stylesheet" type="text/css" />
	<title>Main</title>
	
</head>
<!-- background="bgBig.png" -->
<body bgcolor="#000000">
		
	<%
	HttpSession sess = request.getSession(true);
	int showCombos = (Integer)sess.getAttribute("showCombos");
	int showResults = (Integer)sess.getAttribute("showResults");
	
	if (sess.getAttribute("Error") != null){
		int errorNr = (Integer)sess.getAttribute("ErrorNr");
		if (errorNr == 1){
	%>
			<script language = "javascript" type = "text/javascript">
				alert("Error! Connect to database failed!");
			</script>
	<%	}
		if (errorNr == 2){
	%>
			<script language = "javascript" type = "text/javascript">
				alert("Error! Run the tests failed!");
			</script>
			
	<% 	}
	}%>
		
	<!-- Beallito resz, radio, combo, check, gomb -->
	<div id="fileType">
		<div id="leftSide">
			<div>
				<form method="post" action="LoadDatasServlet">
				<table>
				<tr>
					<td width="60%"></td>
					<td>
					<!-- milyen tipusu fileok vannak -->
					<% ArrayList<String> fileTypesList = new ArrayList<String>();
					String fFormat = (String)sess.getAttribute("fFormat");
					fileTypesList = (ArrayList<String>)sess.getAttribute("fileTypesList");
					for (int i = 0; i < fileTypesList.size(); i++) { 
						if (fFormat.equals(fileTypesList.get(i))){%>
							<input type="radio" name="fileFormat" value="<%=fileTypesList.get(i)%>" checked="checked"><%=fileTypesList.get(i)%><br>
					<%  }else{ %>
							<input type="radio" name="fileFormat" value="<%=fileTypesList.get(i)%>"><%=fileTypesList.get(i)%><br>
					  <%}
						}%>
						<!--  alma<br>korte<br>kutya<br> -->
					</td>
				</tr>
				<tr>
					<td></td>
					<td>
					<input type="submit" value="ok"/>
					</td>
				</tr>
				</table>
			</form>
			</div>
			
			
		</div>
		<div id="rightSide">
			<% if (showCombos == 1) {
			ArrayList<String> datas = new ArrayList<String>();
			datas = (ArrayList<String>)sess.getAttribute("datas");
			ArrayList<String> levelTypesList = new ArrayList<String>();
			levelTypesList = (ArrayList<String>)sess.getAttribute("levelTypesList");
			String lType = (String)sess.getAttribute("lTypes");
			String fFileN = (String)sess.getAttribute("fFileN");
			String sFileN = (String)sess.getAttribute("sFileN");
		%>	<div>
				<form method="post" action="CheckingServlet">
					<select name="firstCombo">
						<%for (int i = 0; i < datas.size(); i++) { 
						if (fFileN.equals(datas.get(i))){%>
							<option value=<%=datas.get(i) %> selected="selected"><%= datas.get(i) %></option>
						<%}else{ %>
							<option value=<%=datas.get(i) %>><%= datas.get(i) %></option>
					<% 	  }
						}%>		
					</select>
			
					<select name="secondCombo">
						<%for (int i = 0; i < datas.size(); i++) { 
						if (sFileN.equals(datas.get(i))){%>
							<option value=<%=datas.get(i) %> selected="selected"><%= datas.get(i) %></option>
						<%}else{ %>
							<option value=<%=datas.get(i) %>><%= datas.get(i) %></option>
					<% 	  }
						}%>	
					</select>
								
					<br><br>
					
					<%for (int i = 0; i < levelTypesList.size(); i++) { 
						if (lType.equals(levelTypesList.get(i))){%>
							<input type="radio" name="originalOrModified" value="<%=levelTypesList.get(i)%>" checked="checked"/><%=levelTypesList.get(i)%> &nbsp; &nbsp; &nbsp;
						<%}else{ %>
							<input type="radio" name="originalOrModified" value="<%=levelTypesList.get(i)%>"/><%=levelTypesList.get(i)%> &nbsp; &nbsp; &nbsp;
					<%	  } 
					}%>
					
			 		<br><br>
			 		<!-- az implementalt,meglevo algorimusok feltuntetese -->
			 		<%ArrayList<String> testTypesList = new ArrayList<String>();
					  testTypesList = (ArrayList<String>)sess.getAttribute("testTypesList");
					  ArrayList<String> checkedTypes = (ArrayList<String>)sess.getAttribute("cTypes");
					  for(int i = 0; i < testTypesList.size(); i++){
					  	if (checkedTypes.contains(testTypesList.get(i))){%>
					  	 	<input type="checkbox" name="simType" value="<%=testTypesList.get(i)%>" checked="checked"><%=testTypesList.get(i)%><br>
					<%  }else{ %>
							<input type="checkbox" name="simType" value="<%=testTypesList.get(i)%>"><%=testTypesList.get(i)%><br>
					<% 	}
					  } %>
					
					
			</div>
			<div align="left">
				<br>
				<input type="checkbox" name="showRows" value="0"/> Show similar rows &nbsp; &nbsp; &nbsp;		
				<input type="submit" value="Run Test"/>
			</div>					
				</form>
		
		<%}%>	
		</div>
		<div class="clear-both"></div>
	</div>
	<!-- Eredmeny megjelenito resz -->
	<div id="results" align="center">
		<%if (showResults == 1) { %>
			<%ArrayList<Object> result = new ArrayList<Object>();
			  result = (ArrayList<Object>)sess.getAttribute("result");
			  ArrayList<String> algTypes = new ArrayList<String>();
			  algTypes = (ArrayList<String>)sess.getAttribute("algTypes");
			  int shRows = (Integer)sess.getAttribute("shRows");
			  		  			  
			%>
			<table BORDER="1" CELLPADDING="1"> 
				<%int i = 0;
			    while (i < result.size() - 2){
					String testTypeInfo = algTypes.get(i/3) + " result: " + result.get(i) + "%";
				%>
					<!-- A teszt tipusa es annak eredmenye -->
					<tr>
						<td  bgcolor="#FF4000"><%=testTypeInfo%></td>
					</tr>
					<%
					 if (shRows == 1){
					  ArrayList<Integer> similarRows = new ArrayList<Integer>();
					  similarRows = (ArrayList<Integer>)result.get(i+1);
			
					  ArrayList<String> rows = new ArrayList<String>();
					  rows = (ArrayList<String>)result.get(i+2);
					  
					  ArrayList<Object> firstFileInfo = new ArrayList<Object>();
					  firstFileInfo = (ArrayList<Object>)result.get(result.size()-2);
					  ArrayList<Object> secondFileInfo = new ArrayList<Object>();
				   	  secondFileInfo = (ArrayList<Object>)result.get(result.size()-1);
					  
					  int k1 = 0;
					  String firstFileName = firstFileInfo.get(k1).toString();
				   	  int firstStart = 0;
					  int firstFileRowsNr = Integer.parseInt(firstFileInfo.get(k1 + 1).toString());
				
					  int j = 0;
					  while( j < rows.size() - 2){
						  String first;
						  String second;
				
						  int k2 = 0;
					  	  String secondFileName = secondFileInfo.get(k2).toString();
				   	  	  int secondStart = 0;
				   	      int secondFileRowsNr = Integer.parseInt(secondFileInfo.get(k2 + 1).toString());   
				
				   	      while (!((similarRows.get(j + 1) > secondStart) && (similarRows.get(j + 1) <= secondFileRowsNr))){
							 k2 += 2;
							 secondFileName = secondFileInfo.get(k2).toString();
							 secondStart = secondFileRowsNr;
							 secondFileRowsNr = Integer.parseInt(secondFileInfo.get(k2 + 1).toString());
						 }
				  
						  if (similarRows.get(j) > firstFileRowsNr){
							  k1 += 2;
							  firstFileName = firstFileInfo.get(k1).toString();
							  firstStart = firstFileRowsNr;
							  firstFileRowsNr = Integer.parseInt(firstFileInfo.get(k1 + 1).toString());
						  }
		
						  first =  firstFileName + ": " +  (similarRows.get(j) - firstStart) + ": " + rows.get(j);
						  second = secondFileName + ": " + (similarRows.get(j+1) - secondStart) + ": " + rows.get(j+1);
				    %>		
						 <tr>
							<td align="left"><%=first %></td>
						    <td align="left"><%=second %></td>
						 </tr>	
					<%   j += 2;		
					 }
			    }	
				i += 3;
			  }%>
			
			</table>
	   <%}%>
   </div>
</body>
</h>